package org.example.logindemo.service.Impl;

import lombok.extern.slf4j.Slf4j;
import org.example.logindemo.bean.RegisterForm;
import org.example.logindemo.bean.RegisterFormDTO;
import org.example.logindemo.bean.RegisterFormVO;
import org.example.logindemo.bean.User;
import org.example.logindemo.mapper.RegisterMapper;
import org.example.logindemo.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RegisterMapper registerMapper;

    public RegisterForm save(RegisterFormDTO registerFormDTO) {

        //获得账号
        String username = registerFormDTO.getUsername();
        //在库中查询有没有账号，有则存在无法创建，无则下一步
        RegisterForm registerForm = registerMapper.getByUsername(username);

        if (registerForm == null) {
            // 账号不存在创建账号
            log.info("账号不存在，添加账号");
            registerMapper.insert(registerFormDTO);

            RegisterForm regForm = new RegisterForm();
            regForm.setMessage("账号创建成功");
            regForm.setSuccess(true);

            return regForm;
        }

        registerForm.setMessage("注册失败,账号已存在");
        registerForm.setSuccess(false);
        return registerForm;

    }
}
